package com.sdgakj.web.manager;

import com.google.common.collect.Maps;
import com.sdgakj.entity.Role;
import com.sdgakj.enums.EnumIsDelete;
import com.sdgakj.service.manager.RoleService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.util.Map;

@Controller
@RequestMapping(value="/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	private static Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	private static final String PAGE_SIZE = "10";
	
	private static Map<String,String> sortTypes =Maps.newLinkedHashMap();
	
	static {
		sortTypes.put("auto", "自动");
	}

	/**
	 * 查询权限列表
	 * @param pageNumber 页号
	 * @param pageSize   页数
	 * @param sortType   排序字段
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
			ServletRequest request){
		logger.info("query the role list...");
		Map<String,Object> searchParams =Servlets.getParametersStartingWith(request, "search_");
		if(searchParams.size()<1){
			searchParams.put("EQ_deltag", "0");
		}
		Page<Role> roles =roleService.getRoles(searchParams,pageNumber,pageSize,sortType);
		
		model.addAttribute("roles",roles);
		model.addAttribute("sortType", sortType);
		model.addAttribute("sortTypes", sortTypes);
		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams,"search_"));
		return "manager/roleList";
	}
	
	/**
	 * 跳转新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/createform",method=RequestMethod.GET)
	public String addRole(Model model){
		model.addAttribute("action","create");
		return "manager/roleForm";
	}
	
	/**
	 * 添加角色
	 * @param Source
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String createRole(Role entity,RedirectAttributes redirectAttributes){
		logger.info("save the role");
		roleService.saveRole(entity);	
		redirectAttributes.addAttribute("message", "添加成功！");
		return "redirect:/role/list";
	}
	
	/**
	 * 跳转修改页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/update/{id}",method=RequestMethod.GET)
	public String updateRole(@PathVariable("id") Long id,Model model){
		Role role =roleService.getRoleById(id);
		model.addAttribute("role", role); //当前要修改的权限
		model.addAttribute("action", "update");
		return "manager/roleForm";
	}
	
	/**
	 * 更新角色
	 * @param Source
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String updateRole(@ModelAttribute("role")Role entity,RedirectAttributes redirectAttributes){
		logger.info("update the role...");
		roleService.updateRole(entity);	
		redirectAttributes.addAttribute("message", "修改成功！");
		return "redirect:/role/list";
	}
	
	/**
	 * 删除角色信息
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public String delete(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		Role role =roleService.getRoleById(id);
		role.setDeltag(EnumIsDelete.DELETED.getCode()); //设置已经禁用
		roleService.saveRole(role);
		redirectAttributes.addFlashAttribute("message", "禁用成功！");
		return "redirect:/role/list";
	}
	
	/**
	 * 恢复角色信息
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/recover/{id}",method=RequestMethod.GET)
	public String recover(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		Role role =roleService.getRoleById(id);
		role.setDeltag(EnumIsDelete.NOTDELETE.getCode()); //设置已经删除
		roleService.saveRole(role);
		redirectAttributes.addFlashAttribute("message", "恢复成功！");
		return "redirect:/role/list";
	}	
	
	@ModelAttribute
	public void getRole(@RequestParam(value ="id",defaultValue = "-1")Long id ,Model model){
		if(id != -1){
			model.addAttribute("role", roleService.getRoleById(id));
		}
	}
	
	/**
	 * Ajax请求校验rolename是否唯一。
	 */
	@RequestMapping(value = "/checkrolename")
	@ResponseBody
	public String checkMobile(@RequestParam("rolename") String rolename,
			@RequestParam(value = "userId", defaultValue = "") String userId) {
		Role role = null;
		if(StringUtils.isNotBlank(userId)){
			role = roleService.getRoleById(Long.valueOf(userId));
		}
		System.out.println(rolename+"-------校验rolename是否唯一--------------rolename");
		Role checkrole = null;
		checkrole = roleService.getRoleByRoleName(rolename); //普通用户的表
		if (checkrole == null) {
			return "true";
		} else {
			if (role != null && checkrole.getCreatedate().equals(role.getCreatedate())) {
				return "true";
			}
			return "false";
		}
	}
	
}
